function getQueryParameter(datatype) {  
    const urlParams = new URLSearchParams(window.location.search);  
    return urlParams.get(datatype);  
}  

// 更新医生信息
function updateDoctorInfo(doctorInfo) {
    const doctorName = document.getElementById('doctor-name');
    const doctorDept = document.getElementById('doctor-dept');
    
    if (doctorInfo) {
        if (doctorName) {
            doctorName.textContent = `${doctorInfo.doctor_name} (${doctorInfo.doctor_id})`;
        }
        if (doctorDept) {
            doctorDept.textContent = `${doctorInfo.dept_name}`;
        }
    }
}

// 页面加载时检查登录状态
document.addEventListener('DOMContentLoaded', function() {
    fetch('/auth/check_auth')
        .then(response => response.json())
        .then(data => {
            if (data.msg === 'success' && data.authenticated) {
                updateDoctorInfo(data.doctor_info);

            }
        })
        .catch(error => console.error('Error:', error));
    
}); 

// 显示医生信息模态框
function showDoctorModal() {
    // 检查是否已经存在模态框
    let modal = document.querySelector('.modal-overlay');
    if (!modal) {
        // 获取当前登录医生的信息
        const doctorName = document.querySelector('.doctorName').textContent;
        const doctorDept = document.querySelector('#doctor-dept').textContent;
        const doctorId = doctorName.match(/\((.*?)\)/)?.[1] || '';
        const name = doctorName.replace(/\s*\(.*?\)\s*/, '');

        // 创建模态框
        const modalHTML = `
            <div class="modal-overlay" id="doctor-modal">
                <div class="doctor-modal">
                    <div class="doctor-header">
                        <div class="doctor-avatar">
                            <i class="fas fa-user-md"></i>
                        </div>
                        <h2>${name}</h2>
                        <p class="doctor-title">${doctorDept}</p>
                        <button class="close-btn" onclick="closeDoctorModal()">
                            <i class="fas fa-times"></i>
                        </button>
                    </div>
                    
                    <div class="doctor-content">
                        <div class="info-section">
                            <div class="info-row">
                                <div class="info-item">
                                    <i class="fas fa-id-card"></i>
                                    <div class="info-text">
                                        <label>工号</label>
                                        <span>${doctorId}</span>
                                    </div>
                                </div>
                                <div class="info-item">
                                    <i class="fas fa-hospital-alt"></i>
                                    <div class="info-text">
                                        <label>所属科室</label>
                                        <span>${doctorDept}</span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <button onclick="logout()" class="logout-btn">
                        <i class="fas fa-sign-out-alt"></i>
                        退出登录
                    </button>
                </div>
            </div>
        `;
        
        // 添加到页面
        document.body.insertAdjacentHTML('beforeend', modalHTML);
        modal = document.querySelector('.modal-overlay');
    }

    // 显示模态框
    if (modal) {
        modal.style.display = 'flex';
        requestAnimationFrame(() => {
            modal.classList.add('show');
        });
    }
}

function closeDoctorModal() {
    const modal = document.querySelector('.modal-overlay');
    if (modal) {
        modal.classList.remove('show');
        setTimeout(() => {
            modal.style.display = 'none';
        }, 300);
    }
}

function logout() {
    if (confirm('确定要退出登录吗？')) {
        fetch('/auth/logout', {
            method: 'POST',
            credentials: 'same-origin'
        })
        .then(response => response.json())
        .then(data => {
            if (data.msg === 'success') {
                window.location.href = '/';
            } else {
                alert(data.error || '退出失败，请稍后重试');
            }
        })
        .catch(error => {
            console.error('Error:', error);
            alert('网络错误，请稍后重试');
        });
    }
}

// 点击模态框外部关闭
document.addEventListener('click', function(event) {
    const modal = document.querySelector('.modal-overlay');
    if (event.target === modal) {
        closeDoctorModal();
    }
});

// ESC键关闭模态框
document.addEventListener('keydown', function(event) {
    if (event.key === 'Escape') {
        closeDoctorModal();
    }
}); 

document.addEventListener('DOMContentLoaded', function() {
    fetch('/api/requests/my')
        .then(response => response.json())
        .then(data => {
            if (data.msg === 'success') {
                if(data.requests.length != 0)
                {
                    icon = document.getElementById("request_icon")
                    icon.src = "/static/pictures/request.png"
                }    

            }
        })
        .catch(error => console.error('Error:', error));
})
